package com.advert.presentation.bean.advert;

import com.advert.model.advert.AdvertEntity;
import com.advert.model.advert.search.*;
import com.advert.model.support.AdvertType;
import com.advert.model.support.ProductType;
import com.advert.presentation.bean.PageAwareBeanBase;
import com.advert.presentation.util.ActionOutcome;
import com.advert.security.AppSecurityManager;
import com.advert.service.advert.AdvertService;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import java.util.Collections;
import java.util.List;


@Name("viewAdvertsBean")
@Scope(ScopeType.PAGE)
public class ViewAdvertsBean extends PageAwareBeanBase {
    @In("AdvertService")
    private AdvertService advertService;
    @In("SecurityManager")
    private AppSecurityManager securityManager;

    private List<AdvertEntity> adverts;
    private List<AdvertEntity> currentUserAdverts;
    private Integer pageCount;

    private String advertType;
    private String productType;
    private String vehicleType;
    private String vehicleCategory;


    // Accessors

    public String getAdvertType() {
        return advertType;
    }

    public void setAdvertType(String advertType) {
        this.advertType = advertType;
    }

    public String getVehicleType() {
        return vehicleType;
    }

    public void setVehicleType(String vehicleType) {
        this.vehicleType = vehicleType;
    }

    public String getVehicleCategory() {
        return vehicleCategory;
    }

    public void setVehicleCategory(String vehicleCategory) {
        this.vehicleCategory = vehicleCategory;
    }

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public List<AdvertEntity> getAdverts() {
        if (adverts == null) {
            adverts = Collections.emptyList();
            if (getAdvertType() != null && !getAdvertType().isEmpty()) {
                AdvertType advertType = null;
                try {
                    advertType = AdvertType.valueOf(getAdvertType());
                } catch (IllegalArgumentException e) {}
                if (getProductType() != null && !getProductType().isEmpty()) {
                    ProductType productType = null;
                    try {
                        productType = ProductType.valueOf(getProductType());
                    } catch (IllegalArgumentException e) {}
                    if (getVehicleType() != null && !getVehicleType().isEmpty()) {
                        if (getVehicleCategory() != null && !getVehicleCategory().isEmpty()) {
                            CriteriaBase criteria = new VehicleCategoryCriteria(advertType, productType, getVehicleCategory());
                            adverts = advertService.getAdverts(criteria, getPageNumber(), getAdvertsToDisplay());
                        } else {
                            CriteriaBase criteria = new VehicleTypeCriteria(advertType, productType, getVehicleType());
                            adverts = advertService.getAdverts(criteria, getPageNumber(), getAdvertsToDisplay());
                        }
                    } else {
                        CriteriaBase criteria = new ProductTypeCriteria(advertType, productType);
                        adverts = advertService.getAdverts(criteria, getPageNumber(), getAdvertsToDisplay());
                    }
                } else {
                    CriteriaBase criteria = new AdvertTypeCriteria(advertType);
                    adverts = advertService.getAdverts(criteria, getPageNumber(), getAdvertsToDisplay());
                }
            }
        }
        return adverts;
    }

    public List<AdvertEntity> getCurrentUserAdverts() {
        if (currentUserAdverts == null) {
            CriteriaBase criteria = new OwnerIdCriteria(securityManager.userId());
            currentUserAdverts = advertService.getAdverts(criteria, getPageNumber(), getAdvertsToDisplay());
        }
        return currentUserAdverts;
    }

    public int getPageCount() {
        if (pageCount == null) {
            pageCount = 0;
            if (getAdvertType() != null && !getAdvertType().isEmpty()) {
                AdvertType advertType = AdvertType.valueOf(getAdvertType());
                pageCount = advertService.getPageCount(new AdvertTypeCriteria(advertType), getAdvertsToDisplay());
            } else if (securityManager.isLoggedIn()) {
                pageCount = advertService.getPageCount(new OwnerIdCriteria(securityManager.userId()), getAdvertsToDisplay());
            }
        }
        return pageCount;
    }


    // Actions

    public String removeAdvert(Integer id) {
        advertService.removeAdvert(id);
        return ActionOutcome.SUCCESS.name();
    }

}
